import { createRouter, createWebHistory } from 'vue-router'
import Register from '../components/Register.vue'
import FileUpload from '../components/FileUpload.vue'
import Login from '../components/Login.vue'
import TaskList from '../components/TaskList.vue'
import Statistics from '../components/Statistics.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录 - 工作任务管理系统'
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      title: '注册 - 工作任务管理系统'
    }
  },
  {
    path: '/upload',
    name: 'Upload',
    component: FileUpload,
    meta: {
      title: '文件上传 - 工作任务管理系统',
      requiresAuth: true
    }
  },
  {
    path: '/tasks',
    name: 'TaskList',
    component: TaskList,
    meta: {
      title: '任务列表 - 工作任务管理系统',
      requiresAuth: true
    }
  },
  {
    path: '/statistics',
    name: 'Statistics',
    component: Statistics,
    meta: {
      title: '数据统计 - 工作任务管理系统',
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('user'))
  
  document.title = to.meta.title || '工作任务管理系统'

  if (to.meta.requiresAuth && !user) {
    next('/login')
  } else {
    next()
  }
})

export default router 